<?php
require_once 'Zend/Db/Table/Abstract.php';
class NoticeModel extends Zend_Db_Table_Abstract
{
	protected $_name = 'notice';
	protected $_id = 'id';
	public function getAllnotice(){
		$db = Zend_Registry::get('connectDB');
		$mySql = $db->select()
					->from('notice')
					->order('id DESC');
		$list = $db->fetchAll($mySql);
		return $list;
	}

	public function getAllNoticeTeacher($id_teacher){
		$db = Zend_Registry::get('connectDB');
		$mySql = $db->select()
					->from('notice')
					->where('id_teacher = ?',$id_teacher)
					->order('id DESC');
		$list = $db->fetchAll($mySql);
		return $list;
	}


	public function selectLimitStudent($count,$limit){
		$db = Zend_Registry::get('connectDB');
		$mysql = $db -> select()
					->from('notice')
					->limit($count,$limit)
					->order("id DESC");
		$list = $db->fetchAll($mysql);
		return  $list;
	}

	public function getNoticeLimitByTeacher($count,$limit,$id_teacher){
		$db = Zend_Registry::get('connectDB');
		$mysql = $db -> select()
					->from('notice')
					->where('id_teacher = ?',$id_teacher)
					->limit($count,$limit)
					->order("id DESC")
					->join(array('sj' => 'subjects'),'sj.id = notice.id_subjects',array('code_sj' => 'sj.code',
									 'name_sj' => 'sj.name'));
		$list = $db->fetchAll($mysql);
		return  $list;
	}

	/*
	 * Ham them notice
	*/
	public function add($exercises){
		$model = new NoticeModel();
		$data = array('title'=>$exercises['title'],
					  'link'=>$exercises['link'],
					  'id_teacher'=>$exercises['id_teacher'],
					  'id_subjects' => $exercises['id_sj'],
					  'content'=>$exercises['content']);
		$id = $model->insert($data);
		return $id;
	}

	/*
	 * Ham chinh sua notice
	*/
	public function edit($exercises){
		$model = new NoticeModel();
		$data = array('title'=>$exercises['title'],
					  'link'=>$exercises['link'],
					  'id_subjects' => $exercises['id_sj'],
					  'id_teacher'=>$exercises['id_teacher'],
					  'content'=>$exercises['content']);
		$id = $model->update($data,"id=".$exercises["id"]);
		return $id;
	}

	/*
	* Ham lay thong tin cua 1 thong bao qua id
	*/

	function getNoticeById($id){
		$db = Zend_Registry::get('connectDB');
		$mySql = $db->select()
					->from('notice')
					->where("id=?",$id);
		$list = $db->fetchAll($mySql);
		return $list;
	}
    
      /*
    *Ham lay thong tin qua id_sv
    */

    public function getDbByStudentId($stu_id){
        $db = Zend_Registry::get('connectDB');
		$mySql = $db->select()
					->from('info_class')
                    ->joinLeft('notice','notice.id_subjects=info_class.id_subjects')
                    ->joinLeft('subjects','subjects.id=info_class.id_subjects')
					->where("id_students=?",$stu_id)
    	            ->order('notice.id DESC');
		$list = $db->fetchAll($mySql);
		return $list;
    }

}